export default class LocalStorageService {
  //#region localStorage
  /**
   * Saves the data on the local client side localStorage.
   * @param key The key string is the identifier of the data.
   * @param data The data which will be saved.
   */
  static saveLocalData(key: string, data: any): void {
    // localStorage.setItem(key, JSON.stringify(data));
    try {
      wx.setStorageSync(key, JSON.stringify(data));
    } catch (e) { }
  }

  /**
   * Loads the specific data from the local client side localStorage.
   * @param key The key string is the identifier of the data.
   * @param defaultValue Optional, will returned if there is no value for provided key found.
   * @return The data object.
   */
  static loadLocalData(key: string, defaultValue?: string): any {
    let jsonValue;

    try {
      jsonValue = wx.getStorageSync(key);
    } catch (e) {
      // Do something when catch error
    }

    if (jsonValue) {
      return JSON.parse(jsonValue);
    }

    return defaultValue;
  }

  /**
   * Removes the specific data from the local client side localStorage.
   * @param key The key string is the identifier of the data.
   * @return The data object.
   */
  static removeLocalData(key: string): void {
    try {
      wx.removeStorageSync(key);
    } catch (e) {
      // Do something when catch error
    }
  }
  //#endregion
}